﻿using NurseStation;
using NurseStation.common;
using NurseStation.DataModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NurseStation.DataModels
{
    public class DatabaseContext
    {
        private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>
        {
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = $"DataSource=" + Path.Combine(Environment.CurrentDirectory, "NurseWork.db;"),
                //ConnectionString = "Data Source=NurseWork.db;",
                DbType = DbType.Sqlite,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,

            });

            // 设置 SqlSugar 日志记录
            db.Aop.OnLogExecuted = (sql, pars) =>
            {
                LogHelper.GetLogHelper().Info(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
            db.Aop.OnError = (exp) => LogHelper.GetLogHelper().Error(exp.Message);
            return db;
        });

        public static SqlSugarClient Db => _db.Value;
    }
}
